// Author: Wilfred Pinto
// Website: http://projectsilverlight.blogspot.com

using System;
using System.Collections.Generic;
using System.Windows.Media;

namespace SimpleViewer_SL.code
{
    /// <summary>
    /// Contains helper/util classes that is used by the controls
    /// </summary>
    [Obsolete("Used for documentation of namespace in Sandcastle Help File Builder", true)]
    internal class NamespaceDoc { }

    /// <summary>
    /// <c>Align</c> enumerator.
    /// Valid values are: Left, Top, Right, Bottom and Center
    /// </summary>
    public enum Align
    {
        /// <summary>
        /// <c>Left</c> alignment
        /// </summary>
        Left = 0,
        /// <summary>
        /// <c>Top</c> alignment
        /// </summary>
        Top = 1,
        /// <summary>
        /// <c>Right</c> alignment
        /// </summary>
        Right = 2,
        /// <summary>
        /// <c>Bottom</c> alignment
        /// </summary>
        Bottom = 3,
        /// <summary>
        /// <c>Center</c> alignment
        /// </summary>
        Center = 4
    }

    /// <summary>
    /// <c>NavPosition</c> enumerator
    /// Valid values are: Left, Top, Right, Bottom
    /// </summary>
    public enum NavPosition
    {
        /// <summary>
        /// <c>Left</c> nav position
        /// </summary>
        Left = 0,
        /// <summary>
        /// <c>Top</c> nav position
        /// </summary>
        Top = 1,
        /// <summary>
        /// <c>Right</c> nav position
        /// </summary>
        Right = 2,
        /// <summary>
        /// <c>Bottom</c> nav position
        /// </summary>
        Bottom = 3
    }

#pragma warning disable 1591
#pragma warning restore 1591

    /// <summary>
    /// This class contains only static helper methods that aid in
    /// the conversion of string values to the appropriate datatype representation
    /// </summary>
    public class SettingsHelper
    {
        /// <summary>
        /// Converts a string value to its hexadecimal <c>Color</c> representation
        /// </summary>
        /// <param name="strValue">The color value in string (hexadecimal - 0xffffff or decimal - 65535) format</param>
        /// <param name="defaultVal">The value to return if strValue is empty</param>
        /// <returns><c>Color</c> - The parsed strValue as Color or defaultVal</returns>
        static public Color HexStringToColorWithDefault(string strValue, Color defaultVal)
        {
            if (!String.IsNullOrEmpty(strValue))
            {
                try
                {
                    strValue.Trim();
                    int frameColor;

                    // strValue would contain either a hexadecimal value or decimal value
                    // examples: 0xffffff, 65535
                    if (strValue.Length > 2 && strValue[1] == 'x')
                    {
                        // Parse string which contains hexadecimal color value
                        frameColor = Int32.Parse(strValue.Substring(2), System.Globalization.NumberStyles.HexNumber);
                    }
                    else
                    {
                        // Parse string which contains decimal color value
                        frameColor = Int32.Parse(strValue);
                    }

                    // B = Blue, G = Green, R = Red
                    byte B = (byte)(frameColor & 255);
                    byte G = (byte)(frameColor & ((256 * 256) - 1) >> 8);
                    byte R = (byte)(frameColor & ((256 * 256 * 256) - 1) >> 16);

                    // Set the opacity to fully opaque
                    return Color.FromArgb(255, R, G, B);
                }
                catch (Exception) { /* Eat up all exceptions and return defaultVal */ }
            }

            // Return defaultVal if strValue is not specified
            return defaultVal;
        }

        /// <summary>
        /// Converts a string value to its <c>NavPosition</c> representation
        /// </summary>
        /// <param name="strValue">The navposition value in string format</param>
        /// <param name="defaultVal">The value to return if strValue is empty or invalid</param>
        /// <returns><c>NavPosition</c> - The parsed strValue as NavPosition or defaultVal</returns>
        static public NavPosition GetNavPositionWithDefault(string strValue, NavPosition defaultVal)
        {
            if (!String.IsNullOrEmpty(strValue))
            {
                strValue.Trim();

                if (strValue.Equals("bottom", StringComparison.OrdinalIgnoreCase))
                {
                    return NavPosition.Bottom;
                }
                else if (strValue.Equals("top", StringComparison.OrdinalIgnoreCase))
                {
                    return NavPosition.Top;
                }
                else if (strValue.Equals("right", StringComparison.OrdinalIgnoreCase))
                {
                    return NavPosition.Right;
                }
            }

            // Return defaultVal if strValue is not specified
            return defaultVal;
        }

        /// <summary>
        /// Converts a string value to its <c>Align</c> representation
        /// </summary>
        /// <param name="bHorizontal">Specifies horizonal or vertical alignment.
        /// The valid values for horizontal alignment are: Center, Left and Right.
        /// The valid values for vertical alignment are: Center, Top and Bottom</param>
        /// <param name="strValue">The navposition value in string format</param>
        /// <param name="defaultVal">The value to return if strValue is empty or invalid</param>
        /// <returns><c>Align</c> - The parsed strValue as Align or defaultVal</returns>
        static public Align GetAlignWithDefault(bool bHorizontal, string strValue, Align defaultVal)
        {
            if (!String.IsNullOrEmpty(strValue))
            {
                strValue.Trim();

                if (strValue.Equals("center", StringComparison.OrdinalIgnoreCase))
                {
                    return Align.Center;
                }
                else if (bHorizontal && strValue.Equals("left", StringComparison.OrdinalIgnoreCase))
                {
                    return Align.Left;
                }
                else if (bHorizontal && strValue.Equals("right", StringComparison.OrdinalIgnoreCase))
                {
                    return Align.Right;
                }
                else if (strValue.Equals("top", StringComparison.OrdinalIgnoreCase))
                {
                    return Align.Top;
                }
                else if (bHorizontal && strValue.Equals("bottom", StringComparison.OrdinalIgnoreCase))
                {
                    return Align.Bottom;
                }
            }

            // Return defaultVal if strValue is not specified
            return defaultVal;
        }

        /// <summary>
        /// Converts a string value to its <c>int</c> representation
        /// </summary>
        /// <param name="strValue">The integer value in string format</param>
        /// <param name="defaultVal">The value to return if strValue is empty or invalid</param>
        /// <returns><c>int</c> - The parsed strValue as int or defaultVal</returns>
        static public int GetAttributeIntWithDefault(string strValue, int defaultVal)
        {
            try
            {
                if (!String.IsNullOrEmpty(strValue))
                    return Int32.Parse(strValue.Trim());
            }
            catch (Exception) { /* Eat up all exceptions and return defaultVal */ }

            // Return defaultVal if strValue is not specified
            return defaultVal;
        }

        /// <summary>
        /// Converts a string value to its <c>string</c> representation
        /// </summary>
        /// <param name="strValue">The string value in string format</param>
        /// <param name="defaultVal">The value to return if strValue is empty</param>
        /// <returns><c>string</c> - The trimmed strValue or defaultVal</returns>
        static public string GetAttributeStringWithDefault(string strValue, string defaultVal)
        {
            if (!String.IsNullOrEmpty(strValue))
                return strValue.Trim();

            // Return defaultVal if strValue is not specified
            return defaultVal;
        }
    }
}